package com.item.manager.serialize;

import com.fasterxml.jackson.annotation.*;

import java.util.Collections;
import java.util.List;

import static java.lang.String.format;

/**
 * Adaptor for deserialization from YAML.
 *
 * Created by Alexey on 3/3/2015.
 */
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", defaultImpl = TemplateAdaptor.class)
@JsonSubTypes({
        @JsonSubTypes.Type(value = TemplateAdaptor.class, name = "template"),
        @JsonSubTypes.Type(value = MixinAdaptor.class, name = "mixin")
})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "name")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class TemplateAdaptor {
    private String name;
    private List<String> tags = Collections.emptyList();

    @JsonIdentityReference
    private List<MixinAdaptor> mixins = Collections.emptyList();

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
    @JsonSubTypes({
            @JsonSubTypes.Type(value = FlagAttributeAdaptor.class, name = "FLAG"),
            @JsonSubTypes.Type(value = NumberAttributeAdaptor.class, name = "NUMBER"),
            @JsonSubTypes.Type(value = RangeAttributeAdapter.class, name = "RANGE"),
            @JsonSubTypes.Type(value = StringAttributeAdaptor.class, name = "STRING")
    })
    private List<AttributeAdaptor> attributes = Collections.emptyList();

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public List<String> getTags() { return tags; }
    public void setTags(List<String> tags) { this.tags = tags; }

    public List<AttributeAdaptor> getAttributes() { return attributes; }
    public void setAttributes(List<AttributeAdaptor> attributes) { this.attributes = attributes; }

    public List<MixinAdaptor> getMixins() { return mixins; }
    public void setMixins(List<MixinAdaptor> mixins) { this.mixins = mixins; }

    @Override public String toString() {
        return format("TemplateAdapter, name:%s", name);
    }
}
